<template>
  <div class="container">
    <el-dialog
      title="新增文章"
      :visible.sync="dialogVisible"
      width="800px"
      @close="onClose"
    >
      <el-form ref="form" :model="formData" label-width="100px" :rules="rules">
        <el-form-item label="文章标题:" prop="title">
          <el-input v-model="formData.title"></el-input>
        </el-form-item>
        <el-form-item label="文章内容:" prop="articleBody">
          <div style="border: 1px solid #ccc">
            <Toolbar
              style="border-bottom: 1px solid #ccc"
              :editor="editor"
              :defaultConfig="toolbarConfig"
              :mode="mode"
            />
            <Editor
              style="height: 300px; overflow-y: hidden"
              v-model="formData.articleBody"
              :defaultConfig="editorConfig"
              :mode="mode"
              @onCreated="onCreated"
            />
          </div>
        </el-form-item>
        <el-form-item label="视频地址:" prop="videoURL">
          <el-input v-model="formData.videoURL"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button v-if="!isEdit" type="primary" @click="checkForm1"
          >确 定</el-button
        >
        <el-button v-else type="primary" @click="checkForm2">编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { quillEditor } from "vue-quill-editor";
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { add, update, detail } from "@/api/hmmm/articles.js";
export default {
  components: {
    Editor,
    Toolbar,
    quillEditor,
  },
  props: {
    isEdit: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      dialogVisible: false,
      formData: {
        title: "",
        articleBody: "",
        videoURL: "",
      },
      rules: {
        title: [{ required: true, message: "请输入文章标题", trigger: "blur" }],
        articleBody: [
          { required: true, message: "请输入文章内容", trigger: "blur" },
        ],
      },
      editor: null,
      toolbarConfig: {},
      mode: "simple", // or 'simple'
      editorConfig: { placeholder: "请输入内容..." },
    };
  },
  methods: {
    // 手动校验表单
    checkForm1() {
      this.$refs.form
        .validate()
        .then(async () => {
          await add(this.formData);
          this.$message.success("添加成功");
          this.dialogVisible = false;
          this.$emit("done");
        })
        .catch(() => {});
    },
    checkForm2() {
      this.$refs.form
        .validate()
        .then(async () => {
          await update(this.formData);
          this.$message.success("编辑成功");
          this.dialogVisible = false;
          this.$emit("done");
        })
        .catch(() => {});
    },
    //打开弹框的方法 让父组件调用
    openDialog() {
      this.dialogVisible = true;
    },
    //富文本编辑器
    onCreated(editor) {
      this.editor = Object.seal(editor); // 一定要用 Object.seal() ，否则会报错
    },
    // 弹窗关闭
    onClose() {
      this.$refs.form.resetFields();
      console.log("关闭");
    },

    // async updateInfo() {

    //   this.$emit("done");
    // },
    // 新增技巧功能
    // 修改回显
    dataBack(data) {
      detail(data).then((res) => {
        this.formData = res.data;
      });
      console.log(this.formData);
      this.openDialog();
    },
  },
};
</script>

<style scoped lang='less'></style>
